﻿using FRMS.BUS;
using FRMS.DAL;
using FRMS.Model;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;

namespace FRMS.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    [Authorize]
    public class VoucherController : ControllerBase
    {
        [Obsolete]
        private readonly IHostingEnvironment _hostingEnvironment;
        private JwtSettings _jwtSettings;
        IHttpContextAccessor _access;
        private IHttpClientFactory _httpClient;
        public VoucherController(IHostingEnvironment hostingEnvironment, IOptions<JwtSettings> jwtSettingsAccesser, IHttpContextAccessor access, IHttpClientFactory httpClient
          )
        {
            _hostingEnvironment = hostingEnvironment;
            _jwtSettings = jwtSettingsAccesser.Value;
            _access = access;
            _httpClient = httpClient;
        }

        /// <summary>
        /// 海康设备一键下发
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("VoucherIssue")]
        public ResponseMessage VoucherIssue(VoucherInfo voucherInfo)
        {
            ResponseMessage message = new ResponseMessage();
            VoucherBUS BUS = new VoucherBUS();
            CertificateLoad certificate = new CertificateLoad();
            string path = _hostingEnvironment.ContentRootPath;
            try
            {
                if (voucherInfo.DeviceState == "在线")
                {
                    string[] strArray = voucherInfo.AuthorizedFloor.Split(',');
                    int[] arr = Array.ConvertAll(strArray, int.Parse);
                    certificate.elevatorAuthFloor = arr;
                    certificate.credenceNo = voucherInfo.CardNO;
                    certificate.CredenceType = 2;
                    certificate.UserType = voucherInfo.OwnerType;
                    certificate.endTime = voucherInfo.EffectiveEndtime;
                    certificate.startTime = voucherInfo.EffectiveStarttime;
                    certificate.userID = voucherInfo.UUID;
                    if (voucherInfo.DeviceType == 1)
                    {
                        if (voucherInfo.CarduploadFlag == 0 && voucherInfo.PicuploadFlag == 0 && voucherInfo.Lssuestate == 0)
                        {
                            message = BUS.PersonAdd(voucherInfo, path);
                        }
                        else
                        {
                            message.errcode = 1;
                            message.errmsg = "已使用过一键下发，请勿重复使用";
                        }
                    }
                    else
                    {
                        message = BUS.VoucherLoad(certificate, voucherInfo.DeviceNO);
                    }
                }
                else
                {
                    message.errcode = 1;
                    message.errmsg = "设备不在线，无法下发";
                }

            }
            catch (Exception e)
            {
                message.errcode = 1;
                message.errmsg = "凭证下发失败";
                message.data = null;
                message.total = 0;
            }
            return message;
        }

        /// <summary>
        /// 海康设备单次下发
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("VoucherIssueSingle")]
        public ResponseMessage VoucherIssueSingle(VoucherInfo voucherInfo)
        {
            ResponseMessage message = new ResponseMessage();
            VoucherBUS BUS = new VoucherBUS();
            CertificateLoad certificate = new CertificateLoad();
            string path = _hostingEnvironment.ContentRootPath;
            try
            {
                if (voucherInfo.DeviceState == "在线")
                {
                    string[] strArray = voucherInfo.AuthorizedFloor.Split(',');
                    int[] arr = Array.ConvertAll(strArray, int.Parse);
                    certificate.elevatorAuthFloor = arr;
                    certificate.credenceNo = voucherInfo.CardNO;
                    certificate.CredenceType = 2;
                    certificate.UserType = voucherInfo.OwnerType;
                    certificate.endTime = Convert.ToDateTime(voucherInfo.EffectiveEndtime).ToString("yyyy-MM-dd HH:mm:ss");
                    certificate.startTime = Convert.ToDateTime(voucherInfo.EffectiveStarttime).ToString("yyyy-MM-dd HH:mm:ss");
                    certificate.userID = voucherInfo.UUID;
                    if (voucherInfo.DeviceType == 1)
                    {
                        message = BUS.PersonAddSingle(voucherInfo, path);
                    }
                    else
                    {
                        message = BUS.VoucherLoad(certificate, voucherInfo.DeviceNO);
                    }
                }
                else
                {
                    message.errcode = 1;
                    message.errmsg = "设备不在线，无法下发";
                }

            }
            catch (Exception e)
            {
                message.errcode = 1;
                message.errmsg = "凭证下发失败";
                message.data = null;
                message.total = 0;
            }
            return message;
        }

        /// <summary>
        /// 凭证删除
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("VoucherDel")]
        public ResponseMessage VoucherDel(VoucherInfo voucherInfo)
        {
            ResponseMessage message = new ResponseMessage();
            VoucherBUS BUS = new VoucherBUS();
            try
            {
                if (voucherInfo.Flag == 1)
                {
                    string type = _access.HttpContext.Session.GetString("ApplyType");
                    if (type == "3")
                    {
                        message = BUS.N71SPVoucherDel(voucherInfo);
                    }
                    else
                    {
                        JLLssueinfoDel info = BUS.JLVoucherDel(voucherInfo);
                        string re = HttpClientPost("http://" + voucherInfo.DeviceIP + ":8011/Request", info);
                        JLLssueinfoReturn jLLssueinfoReturn = JsonConvert.DeserializeObject<JLLssueinfoReturn>(re);
                        if (jLLssueinfoReturn.Name == "personListResponse" && jLLssueinfoReturn.Code == 1)
                        {
                            message = BUS.DelVoucher(voucherInfo);
                        }
                        else if (jLLssueinfoReturn.Name == "personListResponse" && jLLssueinfoReturn.Data.Result == -21 && jLLssueinfoReturn.Code == 1110)
                        {
                            message = BUS.DelVoucher(voucherInfo);
                        }
                        else
                        {
                            message.errcode = 1;
                            message.errmsg = ErrorCode(jLLssueinfoReturn);
                            message.data = null;
                            message.total = 0;
                        }
                    }
                }
                else
                {
                    message = BUS.DelVoucher(voucherInfo);
                }
            }
            catch (Exception e)
            {
                DataClearBUS dataClearBUS = new DataClearBUS();
                ErrorLog errorLog = new ErrorLog();
                errorLog.ErrorMsg = e.ToString();
                errorLog.Operation = "删除凭证";
                dataClearBUS.AddErrorLog(errorLog);
                message.errcode = 1;
                if (e.ToString().Contains("由于连接方在一段时间后没有正确答复或连接的主机没有反应，连接尝试失败"))
                {
                    message.errmsg = "删除失败，检查设备是否在线";
                }
                else
                {
                    message.errmsg = "删除异常";
                }
                message.data = null;
                message.total = 0;
            }
            return message;
        }

        /// <summary>
        /// 海康设备凭证清除
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("VoucherClear")]
        public ResponseMessage VoucherClear(string DeviceNO)
        {
            ResponseMessage message = new ResponseMessage();
            VoucherBUS BUS = new VoucherBUS();
            try
            {
                CertificateClear certificate = new CertificateClear();
                message = BUS.VoucherClear(certificate, DeviceNO);
            }
            catch (Exception e)
            {
                message.errcode = 1;
                message.errmsg = "凭证清除失败";
                message.data = null;
                message.total = 0;
            }
            return message;
        }

        /// <summary>
        /// 凭证绑定
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("BindVoucher")]
        public ResponseMessage VoucherBind(VoucherInfo voucherInfo)
        {
            ResponseMessage message = new ResponseMessage();
            VoucherBUS BUS = new VoucherBUS();
            try
            {
                voucherInfo.projectID = Convert.ToInt32(_access.HttpContext.Session.GetString("ProjectID"));
                message = BUS.AddVoucher(voucherInfo);
            }
            catch (Exception e)
            {
                DataClearBUS dataClearBUS = new DataClearBUS();
                ErrorLog errorLog = new ErrorLog();
                errorLog.ErrorMsg = e.ToString();
                errorLog.Operation = "绑定凭证";
                dataClearBUS.AddErrorLog(errorLog);
                message.errcode = 1;
                message.errmsg = "凭证绑定失败";
                message.data = null;
                message.total = 0;
            }
            return message;
        }

        /// <summary>
        /// 查询凭证
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("QueryVoucher")]
        public ResponseMessage QueryVoucher(VoucherInfo voucherInfo)
        {
            ResponseMessage message = new ResponseMessage();
            VoucherBUS BUS = new VoucherBUS();
            try
            {
                voucherInfo.projectID = Convert.ToInt32(_access.HttpContext.Session.GetString("ProjectID"));
                message = BUS.QueryVoucher(voucherInfo);
            }
            catch (Exception e)
            {
                DataClearBUS dataClearBUS = new DataClearBUS();
                ErrorLog errorLog = new ErrorLog();
                errorLog.ErrorMsg = e.ToString();
                errorLog.Operation = "查询凭证";
                dataClearBUS.AddErrorLog(errorLog);
                message.errcode = 1;
                message.errmsg = "查询凭证异常";
                message.total = 0;
            }
            return message;
        }

        /// <summary>
        /// 载入凭证
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("LoadVoucher")]
        public ResponseMessage LoadVoucher(VoucherInfo voucherInfo)
        {
            ResponseMessage message = new ResponseMessage();
            VoucherBUS BUS = new VoucherBUS();
            try
            {
                message = BUS.LoadVoucher(voucherInfo);
            }
            catch (Exception e)
            {
                DataClearBUS dataClearBUS = new DataClearBUS();
                ErrorLog errorLog = new ErrorLog();
                errorLog.ErrorMsg = e.ToString();
                errorLog.Operation = "载入凭证";
                dataClearBUS.AddErrorLog(errorLog);
                message.errcode = 1;
                message.errmsg = "载入凭证失败";
                message.data = null;
                message.total = 0;
            }
            return message;
        }

        /// <summary>
        /// 绑定卡号
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("BindCard")]
        public ResponseMessage BindCard(VoucherInfo voucherInfo)
        {
            ResponseMessage message = new ResponseMessage();
            VoucherBUS BUS = new VoucherBUS();

            try
            {
                string path = _hostingEnvironment.ContentRootPath;
                message = BUS.BindCard(voucherInfo, path);
            }
            catch (Exception e)
            {
                DataClearBUS dataClearBUS = new DataClearBUS();
                ErrorLog errorLog = new ErrorLog();
                errorLog.ErrorMsg = e.ToString();
                errorLog.Operation = "绑定卡号";
                dataClearBUS.AddErrorLog(errorLog);
                message.errcode = 1;
                message.errmsg = "绑定卡号失败";
                message.data = null;
                message.total = 0;
            }
            return message;
        }

        /// <summary>
        /// 绑定头像
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("BindHead")]
        public ResponseMessage BindHead(VoucherInfo voucherInfo)
        {
            ResponseMessage message = new ResponseMessage();
            VoucherBUS BUS = new VoucherBUS();
            try
            {
                string path = _hostingEnvironment.ContentRootPath;
                message = BUS.BindHead(voucherInfo, path);
            }
            catch (Exception e)
            {
                message.errcode = 1;
                message.errmsg = "绑定卡号失败";
                message.data = null;
                message.total = 0;
            }
            return message;
        }

        /// <summary>
        /// 巨龙凭证下发
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("JLVoucherIssue")]
        public ResponseMessage JLVoucherIssue(int id, string deviceip)
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                string path = _hostingEnvironment.ContentRootPath;
                VoucherBUS BUS = new VoucherBUS();
                string type = _access.HttpContext.Session.GetString("ApplyType");
                if (type == "3")
                {
                    message = BUS.N71SPVoucherIssue(id, deviceip, path);
                }
                else
                {
                    message = BUS.JLVoucherIssue(id, deviceip, path);
                }
            }
            catch (Exception e)
            {
                DataClearBUS dataClearBUS = new DataClearBUS();
                ErrorLog errorLog = new ErrorLog();
                errorLog.ErrorMsg = e.ToString();
                errorLog.Operation = "凭证下发";
                dataClearBUS.AddErrorLog(errorLog);
                message.errcode = 1;
                message.errmsg = "下发异常";
                message.data = null;
                message.total = 0;
            }
            return message;
        }

        /// <summary>
        /// 巨龙凭证清除所有
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("JLVoucherClear")]
        public ResponseMessage JLVoucherClear(int deviceid, int personIdentity)
        {
            ResponseMessage message = new ResponseMessage();
            VoucherBUS BUS = new VoucherBUS();
            try
            {
                message = BUS.JLVoucherClearAll(deviceid, personIdentity);
            }
            catch (Exception e)
            {
                DataClearBUS dataClearBUS = new DataClearBUS();
                ErrorLog errorLog = new ErrorLog();
                errorLog.ErrorMsg = e.ToString();
                errorLog.Operation = "凭证清空";
                dataClearBUS.AddErrorLog(errorLog);
                message.errcode = 1;
                message.errmsg = "清空异常";
                message.data = null;
                message.total = 0;
            }
            return message;
        }

        /// <summary>
        /// 巨龙凭证批量清除
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("JLVoucherBatchDel")]
        public ResponseMessage JLVoucherBatchDel(List<JLLssueinfoDelBatchPerson> list, int deviceid)
        {
            ResponseMessage message = new ResponseMessage();
            JLLssueinfoDelBatch info;
            VoucherBUS BUS = new VoucherBUS();
            try
            {
                DeviceDAL da = new DeviceDAL();
                DeviceInfo deviceInfo = da.LoadDeviceInfo(deviceid);
                info = BUS.JLVoucherDelBatch(list, deviceInfo);
                string re = HttpClientPost("http://" + deviceInfo.DeviceIP + ":8011/Request", info);
                JLLssueinfoReturn jLLssueinfoReturn = JsonConvert.DeserializeObject<JLLssueinfoReturn>(re);
                if (jLLssueinfoReturn.Name == "personListResponse" && jLLssueinfoReturn.Code == 1)
                {
                    message = BUS.DeleteBatchVoucher(list, deviceid);
                }
                else
                {
                    message.errcode = 1;
                    message.errmsg = ErrorCode(jLLssueinfoReturn);
                    message.data = null;
                    message.total = 0;
                }
            }
            catch (Exception e)
            {
                DataClearBUS dataClearBUS = new DataClearBUS();
                ErrorLog errorLog = new ErrorLog();
                errorLog.ErrorMsg = e.ToString();
                errorLog.Operation = "凭证批量清除";
                dataClearBUS.AddErrorLog(errorLog);
                message.errcode = 1;
                message.errmsg = "凭证批量清除异常";
                message.data = null;
                message.total = 0;
            }
            return message;
        }

        public string HttpClientPost(string url, object datajson)
        {
            HttpClient httpClient = new HttpClient();//http对象
            //表头参数
            httpClient.DefaultRequestHeaders.Accept.Clear();
            httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            //转为链接需要的格式
            HttpContent httpContent = new JsonContent(datajson);
            //请求
            HttpResponseMessage response = httpClient.PostAsync(url, httpContent).Result;
            if (response.IsSuccessStatusCode)
            {
                Task<string> t = response.Content.ReadAsStringAsync();
                if (t != null)
                {
                    return t.Result;
                }
            }
            return "";
        }

        public string ErrorCode(JLLssueinfoReturn jLLssueinfoReturn)
        {
            string errormsg = "";
            switch (jLLssueinfoReturn.Code)
            {
                case 1001: errormsg = "设备未注册/注册失败"; break;
                case 1002: errormsg = "JSON 解析错误"; break;
                case 1003: errormsg = "Session无效"; break;
                case 1004: errormsg = "TimeStamp 无效"; break;
                case 1005: errormsg = "Sign无效"; break;
                case 1006: errormsg = "Name无效"; break;
                case 1101: errormsg = "Data无效"; break;
                case 1102: errormsg = "Action无效"; break;
                case 1103: errormsg = "PersonType无效"; break;
                case 1104: errormsg = "PersonId无效"; break;
                case 1105: errormsg = "GetPhoto错误"; break;
                case 1106: errormsg = "Action无效"; break;
                case 1107: errormsg = "PersonInfo/PersonInfoList无效"; break;
                case 1108: errormsg = "PersonName无效"; break;
                case 1109: errormsg = "PersonPhoto无效"; break;
                case 1110:
                    switch (jLLssueinfoReturn.Data.Result)
                    {
                        case 0: errormsg = jLLssueinfoReturn.Message; break;
                        case -1: errormsg = jLLssueinfoReturn.Message; break;
                        case -2: errormsg = "提取特征值失败"; break;
                        case -3: errormsg = "文件名字编号重复"; break;
                        case -4: errormsg = "库满"; break;
                        case -5: errormsg = "添加超时"; break;
                        case -6: errormsg = "参数错误"; break;
                        case -7: errormsg = "文件太大"; break;
                        case -8: errormsg = "存储空间不足"; break;
                        case -9: errormsg = "文件打开失败"; break;
                        case -10: errormsg = "没有数据库"; break;
                        case -11: errormsg = "图片读取失败"; break;
                        case -12: errormsg = "数据库文件损坏"; break;
                        case -13: errormsg = "图片质量差"; break;
                        case -14: errormsg = "图片尺寸错误，宽高不能为奇数"; break;
                        case -15: errormsg = "检测人脸失败"; break;
                        case -16: errormsg = "图片格式错误"; break;
                        case -17: errormsg = "人脸区域错误"; break;
                        case -18: errormsg = "算法创建句柄错误"; break;
                        case -19: errormsg = "设备忙"; break;
                        case -20: errormsg = "文件写入失败"; break;
                        case -21: errormsg = "删除失败(未找到对应的ID删除)"; break;
                        case -22: errormsg = "分配内存失败"; break;
                        case -23: errormsg = "名单数人数为空"; break;
                        case -24: errormsg = "临时名单有效时间错误"; break;
                        case -51: errormsg = "数据异常"; break;
                        case -52: errormsg = "数据标志错误"; break;
                        case -53: errormsg = "数据长度错误"; break;
                        case -54: errormsg = "特征值数据长度错误"; break;
                        case -55: errormsg = "芯片厂商错误"; break;
                        case -56: errormsg = "芯片类型错误"; break;
                        case -57: errormsg = "算法厂商错误"; break;
                        case -58: errormsg = "算法类型错误"; break;
                        case -59: errormsg = "设备类型错误"; break;
                        case -60: errormsg = "算法版本错误"; break;
                        case -61: errormsg = "数据库操作失败"; break;
                        case -62: errormsg = "查无此人"; break;
                        case -5102: errormsg = "Plan字段不正确"; break;
                        case -5103: errormsg = "Date字段不正确"; break;
                        case -5104: errormsg = "重复签到"; break;
                        case -5105: errormsg = "无数据（未发生预案）"; break;
                        case -5106: errormsg = "阈值超出范围"; break;
                        case -5107: errormsg = "缓冲超出范围"; break;
                    };
                    break;
                case 1111: errormsg = jLLssueinfoReturn.Message; break;
                case 1112: errormsg = "设备不在线"; break;
                case 1113: errormsg = "设备处理超时"; break;
                case 1114: errormsg = "内存分配失败"; break;
                case 1115: errormsg = "请求地址无效"; break;
                case 1116: errormsg = "下载文件失败"; break;
                default: errormsg = "异常" + jLLssueinfoReturn.Message; break;
            }
            return errormsg;
        }

        private string ImgToBase64String(string Imagefilename)
        {
            try
            {
                Bitmap bmp = new Bitmap(Imagefilename);

                MemoryStream ms = new MemoryStream();
                bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                byte[] arr = new byte[ms.Length];
                ms.Position = 0;
                ms.Read(arr, 0, (int)ms.Length);
                ms.Close();
                return Convert.ToBase64String(arr);
            }
            catch (Exception ex)
            {
                return "";
            }
        }

        /// <summary>
        /// 查询访客凭证
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("QueryVisitVoucher")]
        public ResponseMessage QueryVisitVoucher(VoucherInfo voucherInfo)
        {
            ResponseMessage message = new ResponseMessage();
            VoucherBUS BUS = new VoucherBUS();
            try
            {
                voucherInfo.projectID = Convert.ToInt32(_access.HttpContext.Session.GetString("ProjectID"));
                voucherInfo.OwnerType = 4;
                message = BUS.QueryVoucher(voucherInfo);
            }
            catch (Exception e)
            {
                DataClearBUS dataClearBUS = new DataClearBUS();
                ErrorLog errorLog = new ErrorLog();
                errorLog.ErrorMsg = e.ToString();
                errorLog.Operation = "查询访客凭证";
                dataClearBUS.AddErrorLog(errorLog);
                message.errcode = 1;
                message.errmsg = "查询访客凭证异常";
                message.total = 0;
            }
            return message;
        }

        /// <summary>
        /// 新增物业凭证
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("AddProVoucher")]
        public ResponseMessage AddProVoucher(OwnerDeviceVoucher ownerDevice)
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                VoucherBUS BUS = new VoucherBUS();
                message = BUS.AddProVoucher(ownerDevice);
            }
            catch (Exception e)
            {
                DataClearBUS dataClearBUS = new DataClearBUS();
                ErrorLog errorLog = new ErrorLog();
                errorLog.ErrorMsg = e.ToString();
                errorLog.Operation = "新增物业凭证";
                dataClearBUS.AddErrorLog(errorLog);
                message.errcode = 1;
                message.errmsg = "新增物业凭证异常";
                message.total = 0;
            }
            return message;
        }

        /// <summary>
        /// 凭证一键下发
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("VoucherIssueOneClick")]
        public ResponseMessage VoucherIssueOneClick(ProVoucherInfo proVoucher)
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                VoucherBUS BUS = new VoucherBUS();
                string path = _hostingEnvironment.ContentRootPath;
                message = BUS.OneClickVoucherIssue(proVoucher, path);
            }
            catch (Exception e)
            {
                DataClearBUS dataClearBUS = new DataClearBUS();
                ErrorLog errorLog = new ErrorLog();
                errorLog.ErrorMsg = e.ToString();
                errorLog.Operation = "一键下发凭证";
                dataClearBUS.AddErrorLog(errorLog);
                message.errcode = 1;
                message.errmsg = "一键下发凭证异常";
                message.total = 0;
            }
            return message;
        }

        /// <summary>
        /// 凭证一键删除
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("VoucherDeleteOneClick")]
        public ResponseMessage VoucherDeleteOneClick(ProVoucherInfo proVoucher)
        {

            ResponseMessage message = new ResponseMessage();
            try
            {
                VoucherBUS BUS = new VoucherBUS();
                message = BUS.OneClickVoucherDel(proVoucher);
            }
            catch (Exception e)
            {
                message.errcode = 1;
                message.errmsg = "一键删除物业凭证异常";
                message.total = 0;
            }
            return message;
        }

        [HttpPost]
        [Route("Test")]
        public ResponseMessage Test()
        {
            ResponseMessage message = new ResponseMessage();
            VoucherBUS voucherBUS = new VoucherBUS();
            //message = voucherBUS.N71SPDeviceLogin("192.168.8.127","admin", "E10ADC3949BA59ABBE56E057F20F883E",1);
            message = voucherBUS.N71SPVoucherIssue(1, "192.168.8.127", "");
            //for (int i = 1; i < 101; i++)
            //{
            //    JLLssueinfo jLLssueinfo = new JLLssueinfo();
            //    jLLssueinfo.Name = "personListRequest";
            //    jLLssueinfo.UUID = "umphu5n6tr62";
            //    jLLssueinfo.Session = "umphu5n6tr62_1651732565";
            //    jLLssueinfo.TimeStamp = GetTimeStamp();
            //    JLLssueinfoData jLLssueinfoData = new JLLssueinfoData();
            //    jLLssueinfoData.Action = "addPerson";
            //    jLLssueinfoData.AddType = 3;
            //    jLLssueinfoData.PersonType = 2;
            //    JLLssueinfoPerson jLLssueinfoPerson = new JLLssueinfoPerson();
            //    jLLssueinfoPerson.PersonId = i.ToString();
            //    jLLssueinfoPerson.PersonName = "业主" + i.ToString();
            //    jLLssueinfoPerson.PersonCover = 1;
            //    jLLssueinfoPerson.Sex = 1;
            //    jLLssueinfoPerson.LimitTime = 1;
            //    jLLssueinfoPerson.StartTime = Convert.ToDateTime("2022-04-28").ToString("yyyy-MM-dd HH:mm:ss");
            //    jLLssueinfoPerson.EndTime = Convert.ToDateTime("2023-04-28").ToString("yyyy-MM-dd HH:mm:ss");
            //    jLLssueinfoPerson.PersonIdentity = 1;
            //    jLLssueinfoPerson.ICCardNo = i.ToString();
            //    JLPersonExtension jLPersonExtension = new JLPersonExtension();
            //    jLPersonExtension.AuthData = "";
            //    jLLssueinfoPerson.PersonExtension = jLPersonExtension;
            //    string url = "";
            //    jLLssueinfoPerson.PersonPhoto = "";
            //    jLLssueinfoPerson.FeatureValue = "";

            //    jLLssueinfoData.PersonInfo = jLLssueinfoPerson;
            //    jLLssueinfo.Data = jLLssueinfoData;

            //    string re = HttpClientPost("http://" + "192.168.8.60" + ":8011/Request", jLLssueinfo);
            //}
            return message;

        }

        public int GetTimeStamp()
        {
            TimeSpan ts = DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0, 0);
            return Convert.ToInt32(ts.TotalSeconds);
        }

        [HttpPost]
        [Route("ToBase64String")]
        public string ToBase64String(string Imagefilename)
        {
            try
            {
                Bitmap bmp = new Bitmap(Imagefilename);

                MemoryStream ms = new MemoryStream();
                bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                byte[] arr = new byte[ms.Length];
                ms.Position = 0;
                ms.Read(arr, 0, (int)ms.Length);
                ms.Close();
                return Convert.ToBase64String(arr);
            }
            catch (Exception ex)
            {
                return "";
            }
        }
    }
}
